/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

var cmSearch;
var gridSearch;
var searchTopicList;
function showSearchPanel(topicTitle)
{
    if(searchTopicList==null)
    {
        searchTopicList = new Ext.data.Store({
            proxy: new Ext.data.HttpProxy({
                url: 'topicListData.jsp'
            }),

            // create reader that reads the Topic records
            reader: new Ext.data.JsonReader({
                root: 'root',
                totalProperty: 'totalCount',
                id: 'threadid',
                fields: [
                    {name: 'title'},
                    {name: 'userName'},
                    {name: 'shortUpdateTime'},
                    {name: 'id'},
                    {name: 'uid'},
                    {name: 'repliedCount'}
                ]
            })
        });
    }
    function renderUser(value, p, record)
    {
        return String.format('<table style="height:25px"><tr><td><div style="height:25px"><div style="height:3px"></div><a href="#" onclick=showUserInfo({2},"{3}") onmouseover="showSimpleViewOfUser({1})" onmouseout="hideSimpleViewOfUser()"><font size="3">{0}</font></a><div style="height:3px"></div></div></td></tr></table>',value,record.data.uid,record.data.uid,record.data.userName);
    }
    function renderTime(value, p, record)
    {
        return String.format('<table style="height:10px;"><tr><td valign="center">{0}</td></tr></table>',value);
    }
    function renderReply(value, p, record)
    {
        return String.format('<table style="height:10px;"><tr><td valign="center">{0}</td></tr></table>',value);
    }
    function renderTitle(value, p, record)
    {
        return String.format('<table style="height:5px;"><tr><td valign="center"><div style="height:9px"></div><font size="3"><a href="#" onclick="viewTopicDetail({1},1);">{0}</a></font></td></tr></table>',value,record.data.id);
    }

    if(cmSearch==null)
    {
        cmSearch = new Ext.grid.ColumnModel([{
               //id: 'topic', // id assigned so we can apply custom css (e.g. .x-grid-col-topic b { color:#333 })
               header: "主题",
               dataIndex: 'title',
               width: 270,
               renderer: renderTitle
            },{
               header: "发帖人",
               dataIndex: 'userName',
               width: 70,
               align: 'center',
               renderer: renderUser
            },{
               header: "更新时间",
               dataIndex: 'shortUpdateTime',
               width: 80,
               align: 'right',
               renderer:renderTime
            },{
               //id: 'last',
               header: "回复/阅读",
               dataIndex: 'repliedCount',
               width: 50,
               align: 'center',
               renderer:renderReply
            }]);
        cmSearch.defaultSortable = true;
    }

    if(gridSearch==null)
    {
        gridSearch = new Ext.grid.GridPanel({
            el:'divSearchResult',
            //height:380,
            //title:'gridview',
            store: searchTopicList,
            cm: cmSearch,
            trackMouseOver:true,
            sm: new Ext.grid.RowSelectionModel({selectRow:Ext.emptyFn}),
            loadMask: true,
            iconCls:'icon-grid',
            autoScroll: true,
            border: false,
            viewConfig: {
                forceFit:true,
                enableRowBody:true,
                showPreview:false,
                getRowClass : function(record, rowIndex, p, store){
                    if(this.showPreview){
                        p.body = '<p>'+record.data.title+'</p><br>';
                        return 'x-grid3-row-expanded';
                    }
                    return 'x-grid3-row-collapsed';
                }
            },
            tbar:[ 
                '查找：',
                new Ext.form.TextField({
                    id:'searchTextField',
                    width:250
                }),
                '<img src="../images/buttons/serach.jpg" width="19" onclick=research() height="21" alt="点击搜索" style="cursor:pointer;" />'
            ],
            bbar: new Ext.PagingToolbar({
                pageSize: 25,
                store: searchTopicList,
                displayInfo: true,
                displayMsg: '搜索到的主题 {0} - {1} of {2}',
                emptyMsg: "无相关的主题"
            })
        });
    }
    //document.getElementById("divData").style.display='';
    //gridSearch.setHeight(document.getElementById("divData").clientHeight);
    gridSearch.render();
    searchTopicList.on('beforeload',function(){
        Ext.apply(
        this.baseParams,
        {
            title: topicTitle
        });
    });
    if(topicTitle!=null)
        searchTopicList.load({params:{start:0, limit:25}});
    tabCount++;
    var panSearch = new Ext.Panel({
        id:'tabSearch',
        //html:document.getElementById('divViewTopic').innerHTML,
        title:"搜索结果",
        //closable:true,
        closeAction:'hide',
        autoScroll:true,
        //items:[divSearchResult]
        el :divSearchResult
    });
    panel.add(panSearch);
    panel.activate('tabSearch');
    //panel.render();
}
function research()
{
    if(document.getElementById('searchTextField').value.length==0)
    {
        TipMessageShow('提示','请输入查询条件');
        return;
    }
    showSearchPanel(document.getElementById('searchTextField').value);
}
/*function refreshSearch()
{
    gridSearch.render();
    searchTopicList.load({params:{start:0, limit:25,title:'1'}});
}*/